package com.yaoweigroup.community.adapter;

import java.util.List;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.yaoweigroup.community.R;
import com.yaoweigroup.community.config.Urls;
import com.yaoweigroup.community.enity.MenuInfo;
import com.yaoweigroup.community.enity.OrderInfo;
import com.yaoweigroup.community.interfaze.Callback;
import com.yaoweigroup.community.interfaze.NewCallback;
import com.yaoweigroup.community.util.BitmapHelp;
import com.yaoweigroup.community.util.LogUtil;
import com.yaoweigroup.community.util.Tools;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MyOrderAdapter extends BaseAdapter {

	private Context context;
	private List<OrderInfo> list;
	private LayoutInflater mInflater;
	public NewCallback callback;
	private BitmapUtils bitmapUtils;
	private BitmapDisplayConfig bigPicDisplayConfig;

	// ，，，，，，

	public MyOrderAdapter(Context context, List<OrderInfo> list) {
		mInflater = LayoutInflater.from(context);
		this.context = context;
		this.list = list;
		bitmapUtils = BitmapHelp.getBitmapUtils(context);
		bigPicDisplayConfig = new BitmapDisplayConfig();
		bigPicDisplayConfig.setLoadFailedDrawable(context.getResources()
				.getDrawable(R.drawable.bucket_no_picture));
		bigPicDisplayConfig.setLoadingDrawable(context.getResources()
				.getDrawable(R.drawable.bucket_no_picture));
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder;
		if (convertView == null) {
			holder = new Holder();
			convertView = mInflater.inflate(
					R.layout.adapter_my_order_list_item, null);
			convertView.setTag(holder);
			holder.icon_iv = (ImageView) convertView.findViewById(R.id.icon_iv);
			holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
			holder.content_tv = (TextView) convertView
					.findViewById(R.id.content_tv);
			holder.money_num_tv = (TextView) convertView
					.findViewById(R.id.money_num_tv);
			holder.reason_tv = (TextView) convertView
					.findViewById(R.id.reason_tv);
			holder.submit_bt = (Button) convertView
					.findViewById(R.id.submit_bt);
			holder.cancel_bt = (Button) convertView
					.findViewById(R.id.cancel_bt);
			holder.orderNum_tv = (TextView) convertView
					.findViewById(R.id.orderNum_tv);
			holder.time_tv = (TextView) convertView.findViewById(R.id.time_tv);
			holder.order_icon_iv = (ImageView) convertView
					.findViewById(R.id.order_icon_iv);
		} else {
			holder = (Holder) convertView.getTag();
		}

		final OrderInfo orderInfo = list.get(position);
		holder.submit_bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				callback.callback(orderInfo.getOrderNum(), 1,
						orderInfo.getOrderPrice(), orderInfo.getDetails());
			}
		});
		holder.cancel_bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				callback.callback(orderInfo.getOrderNum(), 0,
						orderInfo.getOrderPrice(), orderInfo.getDetails());
			}
		});
		holder.time_tv.setText("下单时间：" + orderInfo.getTime());
		List<MenuInfo> menus = orderInfo.getDetails();
		if (menus != null && menus.size() > 0) {
			StringBuffer sb = new StringBuffer();
			for (MenuInfo menu : menus) {
				sb.append(menu.getProductName() + ";单价:" + "<font color='red'>"
						+ menu.getProductPrice() + "</font>" + ";数量:"
						+ "<font color='red'>" + menu.getProductCount()
						+ "</font>");
				sb.append("<br/>");
			}
			holder.content_tv.setText(Html.fromHtml(sb.toString()));

			bitmapUtils.display(holder.order_icon_iv, Urls.imgUrl
					+ menus.get(0).getProductImg(), bigPicDisplayConfig);
		} else {
			holder.content_tv.setText("暂无");
		}
		holder.money_num_tv.setText("￥"
				+ Tools.round(orderInfo.getOrderPrice(), 2));
		int state = orderInfo.getPayState();
		switch (state) {
		case 0:
			// 0:未付款，
			if (orderInfo.getReceivingWay().equals("货到付款")) {
				holder.reason_tv.setVisibility(View.VISIBLE);
				holder.reason_tv.setText("发货中");
				holder.submit_bt.setVisibility(View.GONE);
				holder.cancel_bt.setVisibility(View.GONE);
			} else {
				holder.reason_tv.setVisibility(View.GONE);
				holder.submit_bt.setVisibility(View.VISIBLE);
				holder.cancel_bt.setVisibility(View.VISIBLE);
			}

			break;
		case 1:
			// 1:已付款
			holder.reason_tv.setVisibility(View.VISIBLE);
			holder.submit_bt.setVisibility(View.GONE);
			holder.cancel_bt.setVisibility(View.GONE);
			break;
		case 2:
			// 2:已发货
			holder.reason_tv.setVisibility(View.VISIBLE);
			holder.submit_bt.setVisibility(View.GONE);
			holder.cancel_bt.setVisibility(View.GONE);

			break;
		case 3:
			// 3:已收货
			holder.reason_tv.setVisibility(View.VISIBLE);
			holder.submit_bt.setVisibility(View.GONE);
			holder.cancel_bt.setVisibility(View.GONE);

			break;
		case 4:
			// 4:已完成
			holder.reason_tv.setVisibility(View.VISIBLE);
			holder.submit_bt.setVisibility(View.GONE);
			holder.cancel_bt.setVisibility(View.GONE);

			break;
		case 5:
			// 5:客户取消
			holder.reason_tv.setVisibility(View.VISIBLE);
			holder.submit_bt.setVisibility(View.GONE);
			holder.cancel_bt.setVisibility(View.GONE);

			break;
		case 6:
			// 6:商家取消
			holder.reason_tv.setVisibility(View.VISIBLE);
			holder.submit_bt.setVisibility(View.GONE);
			holder.cancel_bt.setVisibility(View.GONE);

			break;
		case 7:
			// 7:管理员取消
			holder.reason_tv.setVisibility(View.VISIBLE);
			holder.submit_bt.setVisibility(View.GONE);
			holder.cancel_bt.setVisibility(View.GONE);

			break;

		}
		holder.reason_tv.setText(orderInfo.getPayStateDes());
		holder.orderNum_tv.setText("订单号:" + orderInfo.getOrderNum());

		LogUtil.d("wxf", Urls.imgUrl + orderInfo.getProductImg());
		return convertView;
	}

	public final class Holder {

		TextView name_tv;
		ImageView icon_iv;
		TextView content_tv;
		TextView money_num_tv;
		Button submit_bt;
		Button cancel_bt;
		TextView reason_tv;
		TextView time_tv;
		TextView orderNum_tv;
		ImageView order_icon_iv;
	}

	public void setCallback(NewCallback callback) {
		this.callback = callback;

	}

	public void setDataChange(List<OrderInfo> list) {
		this.list = list;
		notifyDataSetChanged();
	}

}
